<template>
  <zh-form-card title="基本信息" :span="24">
    <zh-form ref="formRef" :model="data.formData" label-width="200px" :span="12" :rules="data.rules">
            <zh-form-input
          prop="reportType"
          label="报表类型编码"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.reportType"
      ></zh-form-input>
      <zh-form-input
          prop="reportTypeName"
          label="报表类型名称"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.reportTypeName"
      ></zh-form-input>
      <zh-form-input
          prop="orgId"
          label="所属组织"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.orgId"
      ></zh-form-input>
      <zh-form-input
          prop="appId"
          label="所属应用id"
          type="number"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.appId"
      ></zh-form-input>
      <zh-form-input
          prop="areaCode"
          label="行政区划"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.areaCode"
      ></zh-form-input>
      <zh-form-input
          prop="areaName"
          label="行政区划名称"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.areaName"
      ></zh-form-input>
      <zh-form-input
          prop="sourceType"
          label="来源类型：1 全局 2应用 3行政区划 4单位"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.sourceType"
      ></zh-form-input>
      <zh-form-input
          prop="parentId"
          label="父类报表类型编码"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.parentId"
      ></zh-form-input>
      <zh-form-input
          prop="remarks"
          label="备注说明"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.remarks"
      ></zh-form-input>
      <zh-form-input
          prop="orderNo"
          label="排序号"
          type="number"
          :disabled="props.disabled"
          :span="8"
          v-model="data.formData.orderNo"
      ></zh-form-input>
    </zh-form>
  </zh-form-card>
</template>

<script lang="ts" setup name="reportTypeForm">
import { ElMessage } from 'element-plus';

//------------------定义子组件---------------------------
const formRef = ref();
//------------------接收传入参数(组件)-------------------
const props = defineProps({
  reportTypeId: {
    type: String,
    default: '',
  },
  disabled: {
    type: Boolean,
    default: true,
  },
  btnLoding: Boolean,
});
const emits = defineEmits(['update:btnLoding', 'canner', 'refreshTable']);
//------------------初始化------------------------------
onMounted(() => {
  getDict();
  getDetail();
});
//------------------内部变量定义-------------------------
const data = reactive({
  formData: {} as any,
  detailVisible: true,
  loading: false,
  rules: {
  },
  dictData: {} as any,
  areaCodeDict: [] as any,
  campusParam: {} as any,
});

// 查询数据字典信息
const getDict = () => {
  const params = {} as any;
  params.dictFields = '';
  $api.common.getBatchDict.post(params).then((res) => {
    data.dictData = res.result;
  });
};

//获取详情
const getDetail = () => {
  if (!props.reportTypeId) {
    return;
  }
  data.loading = true;
  $api.common.send
  .post('reportType0002', { reportTypeId: props.reportTypeId })
  .then((res: any) => {
    data.formData = res.result || {};
    data.loading = false;
    data.campusParam.schoolId = data.formData.schoolId;
  })
  .catch(() => {
    data.loading = false;
  });
};

//保存
async function save() {
  let checkFlag = await formRef.value.checkForm();
  if (checkFlag) {
    emits('update:btnLoding', true);
    if (data.formData.reportTypeId) {
      $api.common.send
      .post('reportType0005', data.formData)
      .then(() => {
        ElMessage.success('修改成功');
        emits('update:btnLoding', false);
        emits('canner', (data.detailVisible = false));
        emits('refreshTable');
      })
      .catch(() => {
        emits('update:btnLoding', false);
        emits('canner', (data.detailVisible = true));
      });
    } else {
      $api.common.send
      .post('reportType0004', data.formData)
      .then(() => {
        ElMessage.success('新增成功');
        emits('update:btnLoding', false);
        emits('canner', (data.detailVisible = false));
        emits('refreshTable');
      })
      .catch(() => {
        emits('update:btnLoding', false);
        emits('canner', (data.detailVisible = true));
      });
    }
  }
}

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ save });
</script>
<style lang="scss" scoped></style>
